package cn.zhaoyuening.hadoopdemo;

import cn.zhaoyuening.hadoopdemo.mapper.MaxTemperatureMapper;
import cn.zhaoyuening.hadoopdemo.mapper.WordCounterMapper;
import cn.zhaoyuening.hadoopdemo.reducer.MaxTemperatureReducer;
import cn.zhaoyuening.hadoopdemo.reducer.WordCounterReducer;
import cn.zhaoyuening.hadoopdemo.utils.ConfigureFactory;
import cn.zhaoyuening.hadoopdemo.utils.RunUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.net.URISyntaxException;

public class WordCounter {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
        Configuration conf = ConfigureFactory.getConfiguration();
        String localStr = "H:\\project\\ideaProject\\hadoop-demo\\target\\hadoop-demo-1.0-SNAPSHOT.jar";
        String jarStr = "hdfs:///user/hadoop/jobs/jars/hadoop-demo-1.0-SNAPSHOT.jar";
//        conf.set("mapred.jar",jarStr);
//        RunUtils.upload(localStr,jarStr);
        Job job = Job.getInstance(conf, "word counter");
        job.setJarByClass(WordCounter.class);
        job.setMapperClass(WordCounterMapper.class);
        job.setCombinerClass(WordCounterReducer.class);
        job.setReducerClass(WordCounterReducer.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
